package com.example.util.handler;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author : cwb
 * create at:  2020/8/12  17:26
 * @description: spring工具
 */

@Component
public class SpringContextHandler implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    /**
     * @author : cwb
     * create at:  2020/8/12  17:28
     * @description: 获取Spring上下文中的Bean
     * @Param ：[beanName]
     * @return：java.lang.Object
     * */
    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    public static <T> T getBean(String beanName, Class<T> tClass) {
        return applicationContext.getBean(beanName, tClass);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHandler.applicationContext = applicationContext;
    }
}
